﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KaraokeManagementBUS;
using KaraokeManagementDTO;

namespace KaraokeManagement
{
    public partial class frmKhachHang : DevComponents.DotNetBar.Office2007Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            KhachHangDTO dto = new KhachHangDTO();
            dto.HoTen = txtHoTen.Text;
            dto.GioiTinh = cbGioiTinh.SelectedItem.ToString();
            dto.NgaySinh = dateNgaySinh.Value;
            dto.SoDienThoai = txtSoDienThoai.Text;
            dto.DiaChi = txtDiaChi.Text;
            dto.SoCMND = txtSoCMND.Text;
            dto.LoaiKhachHang_ID = int.Parse(cbLoaiKH.SelectedValue.ToString());

            if (ucQuanLyKhachHang.staticMaKhachHang != -1)
            {
                dto.ID = ucQuanLyKhachHang.staticMaKhachHang;

                if (new KhachHangBUS().capNhatKhachHang(dto))
                {
                    MessageBox.Show("Cập nhật thành công khách hàng !", "Karaoke ABC");
                    ucQuanLyKhachHang.staticMaKhachHang = -1;
                    this.Close();
                    return;
                }
            }

            if (new KhachHangBUS().themKhachHang(dto))
            {
                MessageBox.Show("Thêm thành công 1 khách hàng !", "Karaoke ABC");
                this.Close();
            }
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            if (ucQuanLyKhachHang.staticMaKhachHang != -1)
            {
                KhachHangDTO dto = new KhachHangDTO();
                dto = new KhachHangBUS().layThongTinKhachHang(ucQuanLyKhachHang.staticMaKhachHang);

                //Load dữ liệu form
                txtHoTen.Text = dto.HoTen;
                txtDiaChi.Text = dto.DiaChi;
                txtSoCMND.Text = dto.SoCMND;
                txtSoDienThoai.Text = dto.SoDienThoai;
                cbGioiTinh.SelectedText = dto.GioiTinh;
                cbLoaiKH.SelectedValue = dto.LoaiKhachHang_ID;
                dateNgaySinh.Value = dto.NgaySinh;
            }
            
            initGioiTinh();
            initLoaiKhachHang();
        }

        public void initGioiTinh()
        {
            cbGioiTinh.Items.Add("Nam");
            cbGioiTinh.Items.Add("Nữ");
            cbGioiTinh.SelectedIndex = 0;
        }

        public void initLoaiKhachHang()
        {
            cbLoaiKH.DataSource = new LoaiKhachHangBUS().layDanhSachLoaiKhachHang();
            cbLoaiKH.DisplayMember = "TenLoaiKhachHang";
            cbLoaiKH.ValueMember = "ID";
        }
    }
}
